<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
        <el-row>
            <el-col><el-button type="primary">添加角色</el-button></el-col>
        </el-row>


        <el-table :data="getRolesListData" border>
            <!-- -->
            <!-- 展开列-->
            <el-table-column type="expand">
                <!-- 模板-->
                <template slot-scope="scope">
                    <!-- 一级菜单-->
                    <el-row :class="['bb-border', index === 0 ? 'bt-border' : '', 'dfcenter' ]" v-for="(item1,index) in scope.row.children" :key="item1.id">
                        <el-col :span="5">
                            <el-tag @close="removeTagById(scope.row,item1.id)" closable>{{item1.authName}}</el-tag>
                            <i class="el-icon-caret-right"></i>
                        </el-col>
                        <el-col :span="19">

                            <!-- 二级菜单-->
                            <el-row v-for="(item2, index2) in item1.children" :key="item2.id" :class="[ index2 === 0 ? '' : 'bt-border', 'dfcenter']">
                                <el-col :span="6">
                                    <el-tag @close="removeTagById(scope.row,item2.id)" closable type="success">{{item2.authName}}</el-tag>
                                    <i class="el-icon-caret-right"></i>
                                </el-col>
                                <el-col :span="18">

                                    <!-- 三级菜单-->
                                    <el-tag @close="removeTagById(scope.row,item3.id)" closable type="warning" v-for="item3 in item2.children" :key="item3.id">{{item3.authName}}</el-tag>

                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>
                </template>
            </el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column prop="roleName" label="角色名称"  ></el-table-column>
            <el-table-column prop="roleDesc" label="角色描述" ></el-table-column>
            <el-table-column  label="操作" width="480">
                <template slot-scope="scope">
                    <el-button size="small" type="primary" icon="el-icon-s-tools">编辑</el-button>
                    <el-button size="small" type="warning" icon="el-icon-s-tools">删除</el-button>
                    <el-button size="small" type="danger" icon="el-icon-s-tools" @click="assginRights(scope.row)">分配权限</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>


    <!--分配权限的对话框 -->
    <el-dialog
    title="分配权限"
    :visible.sync="assginDialogVisible"
    width="50%"
    @close="setDefaultId">
    <el-tree
    :data="treeData"
    show-checkbox
    node-key="id"
    :default-checked-keys="defaultId"
    default-expand-all
    :props="defaultAssginRightProps"
    ref="RightTree">
    </el-tree>
    <span slot="footer" class="dialog-footer">
        <el-button @click="assginDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setAssignRight">确 定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
import {requestRolesData, requestDeletRoleRight,requestAssginRight,requestSetAssign} from '@/network/power.js'
export default {
    name: 'roles',
    data() {
        return {
            //角色列表数据
            getRolesListData:[],
            //控制分配权限的显示和隐藏
            assginDialogVisible: false,
            treeData:[],
            defaultAssginRightProps: {
                children: 'children',
                label: 'authName'
            },
            //默认展开的Id
            defaultId:[],
            //保存当前点击的角色ID
            roleId:''
        }
    }, 
    created() {
        this.getRolseData()
    },
    methods: {
        async getRolseData(){
            const {data} = await requestRolesData()
            this.getRolesListData = data.data     
        },
        //删除权限函数
        async removeTagById(row, rightId){
            const res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then((res) => {
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                return res
                }).catch(err => err);          
            if(res !== "confirm") return this.$message.info("取消了删除")
            //发送网络请求，删除权限
            let {data} = await requestDeletRoleRight({url:`roles/${row.id}/rights/${rightId}`})
            row.children = data.data
            this.defaultId = this.defaultId.push(rightId)
            },

        //分配权限函数
        async assginRights(row){
            const {data} = await requestAssginRight()
            if(data.meta.status !== 200) return this.$message.error('获取用户权限失败')
            this.treeData = data.data
            this.roleId = row.id
            this.getRightId(row, this.defaultId)
           

            this.assginDialogVisible = true
        },
        //递归获取权限列表ID
        getRightId(node, arr){

            if(!node.children){
                return arr.push(node.id)
            }
            node.children.forEach( item =>
                this.getRightId(item,arr)
            )
        },
        //把默认数组中的ID值清空
        setDefaultId(){
            this.defaultId = []
        },
        //设置权限函数
        async setAssignRight(){
            //获取所有半展开和全展开的id
            let rids =[...this.$refs.RightTree.getCheckedKeys(),...this.$refs.RightTree.getHalfCheckedKeys()]
            rids = rids.join(',')
            const {data} = await requestSetAssign({url:`roles/${this.roleId}/rights`,data:{rids}})
            if(data.meta.status !== 200) return this.$message.error('用户授权失败')
            this.getRolseData()
            console.log(rids);
            
            this.assginDialogVisible = false
            
            
        }


            
        }
}
</script>

<style lang="less" scoped>
    .el-tag {
        margin: 7px;
    }
    .bt-border {
        border-top: 1px solid #ccc;
    }
    .bb-border {
        border-bottom: 1px solid #ccc;
    }
    .dfcenter{
        display: flex;
        align-items: center;
    }
</style>
